<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java异步编程与ExecutorService详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d3748;
            color: #f7fafc;
            border-radius: 0.5rem;
            position: relative;
        }
        .copy-btn {
            position: absolute;
            right: 0.5rem;
            top: 0.5rem;
            background-color: rgba(255,255,255,0.1);
            border: none;
            color: #f7fafc;
            border-radius: 0.25rem;
            padding: 0.25rem 0.5rem;
            cursor: pointer;
            transition: all 0.2s;
        }
        .copy-btn:hover {
            background-color: rgba(255,255,255,0.2);
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .section-divider {
            width: 100%;
            height: 1px;
            background: linear-gradient(90deg, transparent 0%, rgba(0,0,0,0.1) 50%, transparent 100%);
            margin: 3rem 0;
        }
        .highlight-box {
            background-color: #f0f9ff;
            border-left: 4px solid #3b82f6;
            padding: 1.5rem;
            border-radius: 0 0.5rem 0.5rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Java异步编程与ExecutorService</h1>
                    <p class="text-xl mb-8 opacity-90">探索现代Java应用中高效任务调度的核心工具</p>
                    <div class="flex space-x-4">
                        <div class="flex items-center">
                            <i class="fas fa-bolt text-yellow-300 mr-2"></i>
                            <span>提升响应性</span>
                        </div>
                        <div class="flex items-center">
                            <i class="fas fa-cogs text-blue-200 mr-2"></i>
                            <span>简化线程管理</span>
                        </div>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="relative">
                        <div class="absolute top-0 left-0 w-full h-full bg-blue-500 rounded-xl transform rotate-6"></div>
                        <div class="relative bg-white text-gray-800 p-6 rounded-xl shadow-xl">
                            <div class="flex items-center mb-4">
                                <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-green-500"></div>
                            </div>
                            <pre class="text-sm font-mono"><code>// 创建固定大小线程池
ExecutorService executor = 
    Executors.newFixedThreadPool(4);

// 提交异步任务
Future&lt;Integer&gt; future = 
    executor.submit(() -> {
        Thread.sleep(1000);
        return 42;
    });

// 获取结果
System.out.println(
    "结果: " + future.get());</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 leading-relaxed">
                在现代的Java应用中，异步编程是一项重要的技术，它能有效提高程序的响应性，避免主线程被长时间的阻塞操作拖慢速度。对于处理异步任务，<code class="bg-blue-100 text-blue-800 px-1 rounded">ExecutorService</code>是Java中最常用的工具之一。它是<code class="bg-blue-100 text-blue-800 px-1 rounded">java.util.concurrent</code>包中提供的一个接口，旨在简化线程管理和任务调度的过程。
            </p>
        </section>

        <!-- 1. What is ExecutorService -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">01</span>
                <h2 class="text-3xl font-bold">什么是ExecutorService？</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <p class="mb-6 text-gray-700">
                        在多线程编程中，我们通常会创建一个新的线程来执行某个任务。手动管理线程会涉及到线程的创建、管理以及关闭等一系列问题。为了简化这些操作，Java提供了<code class="bg-blue-100 text-blue-800 px-1 rounded">Executor</code>框架，它可以帮助我们更轻松地管理线程池，调度和执行任务。
                    </p>
                    <p class="mb-6 text-gray-700">
                        <code class="bg-blue-100 text-blue-800 px-1 rounded">ExecutorService</code>是<code class="bg-blue-100 text-blue-800 px-1 rounded">Executor</code>接口的一个子接口，提供了许多便捷的方法来管理任务的执行。常用的任务调度和线程池管理功能都包含在<code class="bg-blue-100 text-blue-800 px-1 rounded">ExecutorService</code>中。
                    </p>
                    
                    <div class="highlight-box mb-6">
                        <div class="flex items-start">
                            <div class="mr-4 text-blue-600">
                                <i class="fas fa-info-circle text-xl"></i>
                            </div>
                            <div>
                                <h3 class="font-bold text-lg mb-2">关键概念</h3>
                                <p>ExecutorService充当了"任务执行器"的角色，它将任务的提交与执行解耦，开发者只需要关注任务的创建和提交，而不需要关心线程的创建和管理。</p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Concept Diagram -->
                    <div class="mt-8">
                        <div class="mermaid">
                            graph LR
                                A[任务提交] --> B[ExecutorService]
                                B --> C[线程池管理]
                                B --> D[任务调度]
                                C --> E[线程创建/销毁]
                                D --> F[任务执行]
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 2. Basic Concepts -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">02</span>
                <h2 class="text-3xl font-bold">ExecutorService的基本概念</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Core Features -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-3 rounded-full mr-4">
                                <i class="fas fa-cogs text-blue-600"></i>
                            </div>
                            <h3 class="text-xl font-bold">核心功能</h3>
                        </div>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                                <span><strong>任务调度</strong>：将任务提交给线程池去执行</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                                <span><strong>任务执行结果管理</strong>：通过Future对象获取结果或检查状态</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                                <span><strong>任务关闭</strong>：优雅地关闭线程池</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <!-- Common Implementations -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-3 rounded-full mr-4">
                                <i class="fas fa-layer-group text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-bold">常见实现类</h3>
                        </div>
                        <div class="space-y-4">
                            <div class="p-4 bg-gray-50 rounded-lg">
                                <h4 class="font-bold text-blue-600 mb-1">ThreadPoolExecutor</h4>
                                <p class="text-sm text-gray-600">最常用的线程池实现，提供核心配置选项</p>
                            </div>
                            <div class="p-4 bg-gray-50 rounded-lg">
                                <h4 class="font-bold text-blue-600 mb-1">ScheduledThreadPoolExecutor</h4>
                                <p class="text-sm text-gray-600">用于周期性任务调度，执行定时任务</p>
                            </div>
                            <div class="p-4 bg-gray-50 rounded-lg">
                                <h4 class="font-bold text-blue-600 mb-1">Executors</h4>
                                <p class="text-sm text-gray-600">通过静态方法简化线程池创建</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- 3. Creating ExecutorService -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">03</span>
                <h2 class="text-3xl font-bold">创建ExecutorService</h2>
            </div>
            
            <p class="mb-8 text-gray-700">
                通过<code class="bg-blue-100 text-blue-800 px-1 rounded">Executors</code>工具类，我们可以方便地创建各种类型的线程池。以下是常见的线程池创建方法：
            </p>
            
            <div class="grid md:grid-cols-3 gap-6">
                <!-- Fixed Thread Pool -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="bg-blue-600 text-white p-4">
                        <h3 class="font-bold">固定大小线程池</h3>
                    </div>
                    <div class="p-6">
                        <div class="code-block p-4 mb-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>ExecutorService executor = 
    Executors.newFixedThreadPool(4);</code></pre>
                        </div>
                        <p class="text-sm text-gray-600">
                            <code>newFixedThreadPool(int nThreads)</code>方法会创建一个固定线程数的线程池。线程池中的线程数是固定的，最多会创建指定数量的线程来执行任务。
                        </p>
                        <div class="mt-4 flex items-center text-sm text-blue-600">
                            <i class="fas fa-info-circle mr-2"></i>
                            <span>适用于负载较重的服务器应用</span>
                        </div>
                    </div>
                </div>
                
                <!-- Cached Thread Pool -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="bg-purple-600 text-white p-4">
                        <h3 class="font-bold">缓存线程池</h3>
                    </div>
                    <div class="p-6">
                        <div class="code-block p-4 mb-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>ExecutorService executor = 
    Executors.newCachedThreadPool();</code></pre>
                        </div>
                        <p class="text-sm text-gray-600">
                            <code>newCachedThreadPool()</code>方法会创建一个缓存线程池。这个线程池根据需要创建新线程，如果线程池中的线程空闲时间超过60秒，会被自动回收。
                        </p>
                        <div class="mt-4 flex items-center text-sm text-purple-600">
                            <i class="fas fa-info-circle mr-2"></i>
                            <span>适用于执行很多短期异步任务</span>
                        </div>
                    </div>
                </div>
                
                <!-- Single Thread Executor -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="bg-green-600 text-white p-4">
                        <h3 class="font-bold">单线程执行器</h3>
                    </div>
                    <div class="p-6">
                        <div class="code-block p-4 mb-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>ExecutorService executor = 
    Executors.newSingleThreadExecutor();</code></pre>
                        </div>
                        <p class="text-sm text-gray-600">
                            <code>newSingleThreadExecutor()</code>方法会创建一个只有单个线程的线程池，任务会按顺序依次执行。
                        </p>
                        <div class="mt-4 flex items-center text-sm text-green-600">
                            <i class="fas fa-info-circle mr-2"></i>
                            <span>适用于需要顺序执行任务的场景</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 4. Submitting Tasks -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">04</span>
                <h2 class="text-3xl font-bold">提交任务给线程池</h2>
            </div>
            
            <p class="mb-8 text-gray-700">
                创建好<code class="bg-blue-100 text-blue-800 px-1 rounded">ExecutorService</code>对象后，我们可以通过<code class="bg-blue-100 text-blue-800 px-1 rounded">submit()</code>或<code class="bg-blue-100 text-blue-800 px-1 rounded">execute()</code>方法来提交任务。
            </p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- submit() Method -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-3 rounded-full mr-4">
                                <i class="fas fa-paper-plane text-blue-600"></i>
                            </div>
                            <h3 class="text-xl font-bold">submit()方法</h3>
                        </div>
                        <p class="mb-4 text-gray-700">
                            <code>submit()</code>方法用于提交一个任务，并返回一个<code>Future</code>对象。<code>Future</code>可以用于获取任务的执行结果或者检查任务的执行状态。
                        </p>
                        <div class="code-block p-4 mb-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>ExecutorService executor = 
    Executors.newFixedThreadPool(4);

Callable&lt;Integer&gt; task = () -> {
    System.out.println("执行异步任务");
    return 42;
};

Future&lt;Integer&gt; future = 
    executor.submit(task);</code></pre>
                        </div>
                    </div>
                </div>
                
                <!-- execute() Method -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-green-100 p-3 rounded-full mr-4">
                                <i class="fas fa-play text-green-600"></i>
                            </div>
                            <h3 class="text-xl font-bold">execute()方法</h3>
                        </div>
                        <p class="mb-4 text-gray-700">
                            <code>execute()</code>方法提交一个<code>Runnable</code>任务，并没有返回值。它适用于那些不需要任务执行结果的场景。
                        </p>
                        <div class="code-block p-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>ExecutorService executor = 
    Executors.newFixedThreadPool(4);

Runnable task = () -> 
    System.out.println("执行无返回值的异步任务");

executor.execute(task);</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 5. Using Future -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">05</span>
                <h2 class="text-3xl font-bold">使用Future获取任务执行结果</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-8">
                    <p class="mb-6 text-gray-700">
                        <code>Future</code>是<code>ExecutorService.submit()</code>方法返回的对象，代表一个异步计算的结果。我们可以通过<code>Future</code>来获取任务的执行结果，也可以检查任务是否完成、是否被取消。
                    </p>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <!-- Get Result -->
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <h3 class="font-bold text-blue-600 mb-3">获取任务结果</h3>
                            <div class="code-block p-4 mb-3">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>Integer result = future.get();</code></pre>
                            </div>
                            <p class="text-sm text-gray-600">
                                <code>get()</code>方法用于获取任务执行的结果。这个方法会阻塞，直到任务完成并返回结果。
                            </p>
                        </div>
                        
                        <!-- Check Completion -->
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <h3 class="font-bold text-blue-600 mb-3">判断任务是否完成</h3>
                            <div class="code-block p-4 mb-3">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>if (future.isDone()) {
    // 任务已完成
}</code></pre>
                            </div>
                            <p class="text-sm text-gray-600">
                                可以通过<code>isDone()</code>方法判断任务是否执行完成。
                            </p>
                        </div>
                        
                        <!-- Cancel Task -->
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <h3 class="font-bold text-blue-600 mb-3">取消任务</h3>
                            <div class="code-block p-4 mb-3">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>boolean cancelled = 
    future.cancel(true);</code></pre>
                            </div>
                            <p class="text-sm text-gray-600">
                                如果任务还没有执行，可以通过<code>cancel()</code>方法取消任务。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Future State Diagram -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-bold mb-4">Future生命周期状态</h3>
                    <div class="mermaid">
                        stateDiagram-v2
                            [*] --> New: 创建Future
                            New --> Running: 任务开始执行
                            Running --> Completed: 任务完成
                            Running --> Cancelled: 任务被取消
                            New --> Cancelled: 任务被取消
                    </div>
                </div>
            </div>
        </section>

        <!-- 6. Shutting Down -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">06</span>
                <h2 class="text-3xl font-bold">优雅地关闭线程池</h2>
            </div>
            
            <p class="mb-8 text-gray-700">
                在任务完成后，应该关闭线程池。否则，线程池会一直运行，占用系统资源。
            </p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- shutdown() -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-3 rounded-full mr-4">
                                <i class="fas fa-power-off text-blue-600"></i>
                            </div>
                            <h3 class="text-xl font-bold">shutdown()方法</h3>
                        </div>
                        <p class="mb-4 text-gray-700">
                            <code>shutdown()</code>方法会优雅地关闭线程池，拒绝接受新任务，等待所有已提交的任务执行完毕后关闭。
                        </p>
                        <div class="code-block p-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>executor.shutdown();</code></pre>
                        </div>
                    </div>
                </div>
                
                <!-- shutdownNow() -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-red-100 p-3 rounded-full mr-4">
                                <i class="fas fa-stop-circle text-red-600"></i>
                            </div>
                            <h3 class="text-xl font-bold">shutdownNow()方法</h3>
                        </div>
                        <p class="mb-4 text-gray-700">
                            <code>shutdownNow()</code>方法会立即停止线程池中的所有任务，并返回尚未执行的任务列表。
                        </p>
                        <div class="code-block p-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>List&lt;Runnable&gt; remainingTasks = 
    executor.shutdownNow();</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 7. ScheduledExecutorService -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">07</span>
                <h2 class="text-3xl font-bold">使用ScheduledExecutorService进行定时任务调度</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-8">
                    <p class="mb-6 text-gray-700">
                        <code>ScheduledExecutorService</code>是<code>ExecutorService</code>的一个子接口，提供了定时和周期性任务的调度。常见的应用场景包括定时任务和周期性任务。
                    </p>
                    
                    <div class="grid md:grid-cols-2 gap-8">
                        <!-- Schedule -->
                        <div>
                            <h3 class="text-xl font-bold mb-4">延迟执行任务</h3>
                            <p class="mb-4 text-gray-700">
                                如果我们希望任务延迟一段时间后执行，可以使用<code>schedule()</code>方法。
                            </p>
                            <div class="code-block p-4 mb-4">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>ScheduledExecutorService scheduler = 
    Executors.newScheduledThreadPool(2);

Runnable task = () -> 
    System.out.println("延迟执行的任务");

scheduler.schedule(task, 5, TimeUnit.SECONDS);</code></pre>
                            </div>
                        </div>
                        
                        <!-- Schedule at Fixed Rate -->
                        <div>
                            <h3 class="text-xl font-bold mb-4">定期执行任务</h3>
                            <p class="mb-4 text-gray-700">
                                如果希望任务定期执行，可以使用<code>scheduleAtFixedRate()</code>方法。
                            </p>
                            <div class="code-block p-4">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>Runnable task = () -> 
    System.out.println("定期执行的任务");

scheduler.scheduleAtFixedRate(
    task, 0, 5, TimeUnit.SECONDS);</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Scheduling Types Diagram -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-bold mb-4">定时任务执行模式</h3>
                    <div class="mermaid">
                        gantt
                            title 定时任务执行模式对比
                            dateFormat  HH:mm:ss
                            axisFormat %H:%M:%S
                            
                            section 延迟执行
                            任务1 :a1, 00:00:00, 5s
                            section 固定频率
                            任务2 :a2, 00:00:00, 5s
                            任务2 :a3, after a2, 5s
                            任务2 :a4, after a3, 5s
                    </div>
                </div>
            </div>
        </section>

        <!-- 8. Performance Optimization -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">08</span>
                <h2 class="text-3xl font-bold">使用ExecutorService优化性能</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <p class="mb-6 text-gray-700">
                        通过合理配置<code>ExecutorService</code>，可以有效提升程序的性能。以下是一些优化的建议：
                    </p>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <!-- Thread Pool Size -->
                        <div class="bg-blue-50 p-6 rounded-lg">
                            <h3 class="font-bold text-blue-600 mb-3">合理选择线程池大小</h3>
                            <p class="text-sm text-gray-600 mb-3">
                                线程池大小的选择应根据实际情况进行调整。过小的线程池可能导致任务积压，过大的线程池则可能带来线程切换的开销。
                            </p>
                            <div class="code-block p-4">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>int corePoolSize = 
    Runtime.getRuntime()
        .availableProcessors();</code></pre>
                            </div>
                        </div>
                        
                        <!-- Task Splitting -->
                        <div class="bg-purple-50 p-6 rounded-lg">
                            <h3 class="font-bold text-purple-600 mb-3">任务的合理拆分</h3>
                            <p class="text-sm text-gray-600">
                                对于较大的任务，应该尽可能将其拆分成多个小任务，提交给线程池并行执行。这样可以提高系统的并发度和响应性。
                            </p>
                            <div class="mt-4 flex justify-center">
                                <i class="fas fa-object-group text-3xl text-purple-400"></i>
                            </div>
                        </div>
                        
                        <!-- Cached Thread Pool -->
                        <div class="bg-green-50 p-6 rounded-lg">
                            <h3 class="font-bold text-green-600 mb-3">使用缓存线程池</h3>
                            <p class="text-sm text-gray-600">
                                当任务量不确定，且任务的执行时间不长时，可以使用<code>newCachedThreadPool()</code>创建一个缓存线程池。它能够根据任务数量动态调整线程池的大小。
                            </p>
                            <div class="mt-3">
                                <div class="flex items-center text-sm text-green-600">
                                    <i class="fas fa-arrows-alt-v mr-2"></i>
                                    <span>自动扩展和收缩线程池大小</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 9. Practical Case -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <span class="text-4xl font-bold text-blue-600 mr-4">09</span>
                <h2 class="text-3xl font-bold">实战案例：异步处理任务调度</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <p class="mb-6 text-gray-700">
                        我们通过一个具体的实战案例来演示如何使用<code>ExecutorService</code>进行异步任务调度。假设我们有一个订单处理系统，其中涉及到多个异步任务，比如订单验证、库存检查、支付处理等。
                    </p>
                    
                    <!-- Order Processing Flow -->
                    <div class="mb-8">
                        <h3 class="text-xl font-bold mb-4">订单处理流程</h3>
                        <div class="mermaid">
                            graph TD
                                A[订单验证] -->|通过| B[库存检查]
                                B -->|通过| C[支付处理]
                                A -->|失败| D[流程终止]
                                B -->|失败| D
                                C -->|成功| E[订单完成]
                                C -->|失败| D
                        </div>
                    </div>
                    
                    <!-- Code Examples -->
                    <div class="grid md:grid-cols-3 gap-6">
                        <!-- Order Validation -->
                        <div>
                            <h4 class="font-bold text-blue-600 mb-3">订单验证</h4>
                            <div class="code-block p-4">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>Callable&lt;Boolean&gt; validateTask = 
    () -> {
        System.out.println("验证订单...");
        Thread.sleep(2000);
        return true;
    };

Future&lt;Boolean&gt; validationFuture = 
    executor.submit(validateTask);</code></pre>
                            </div>
                        </div>
                        
                        <!-- Stock Check -->
                        <div>
                            <h4 class="font-bold text-blue-600 mb-3">库存检查</h4>
                            <div class="code-block p-4">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>Callable&lt;Boolean&gt; stockTask = 
    () -> {
        System.out.println("检查库存...");
        Thread.sleep(1000);
        return true;
    };

Future&lt;Boolean&gt; stockFuture = 
    executor.submit(stockTask);</code></pre>
                            </div>
                        </div>
                        
                        <!-- Payment -->
                        <div>
                            <h4 class="font-bold text-blue-600 mb-3">支付处理</h4>
                            <div class="code-block p-4">
                                <button class="copy-btn" onclick="copyCode(this)">
                                    <i class="fas fa-copy"></i>
                                </button>
                                <pre><code>Callable&lt;Boolean&gt; paymentTask = 
    () -> {
        System.out.println("处理支付...");
        Thread.sleep(3000);
        return true;
    };

Future&lt;Boolean&gt; paymentFuture = 
    executor.submit(paymentTask);</code></pre>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Wait for Results -->
                    <div class="mt-8">
                        <h4 class="font-bold text-blue-600 mb-3">等待任务完成</h4>
                        <div class="code-block p-4">
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="fas fa-copy"></i>
                            </button>
                            <pre><code>// 等待所有任务完成
System.out.println("订单验证结果: " + 
    validationFuture.get());
System.out.println("库存检查结果: " + 
    stockFuture.get());
System.out.println("支付处理结果: " + 
    paymentFuture.get());

// 关闭线程池
executor.shutdown();</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-blue-500 to-purple-600 rounded-xl p-8 text-white">
                <h2 class="text-3xl font-bold mb-6">总结</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-bold mb-4">ExecutorService核心优势</h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1"></i>
                                <span>简化线程管理和任务调度</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1"></i>
                                <span>提高应用程序响应性和吞吐量</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1"></i>
                                <span>提供丰富的线程池配置选项</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1"></i>
                                <span>支持异步任务结果获取和状态检查</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-4">最佳实践建议</h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-lightbulb mr-2 mt-1"></i>
                                <span>根据任务类型选择合适的线程池</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-lightbulb mr-2 mt-1"></i>
                                <span>合理设置线程池大小</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-lightbulb mr-2 mt-1"></i>
                                <span>任务完成后及时关闭线程池</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-lightbulb mr-2 mt-1"></i>
                                <span>考虑使用Future处理异步结果</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white">技术小馆</h3>
                    <p class="text-sm mt-1">专注于Java技术分享</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-white transition-colors">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Copy code function
        function copyCode(button) {
            const codeBlock = button.parentElement.querySelector('code');
            const text = codeBlock.textContent;
            navigator.clipboard.writeText(text).then(() => {
                const originalIcon = button.innerHTML;
                button.innerHTML = '<i class="fas fa-check"></i>';
                setTimeout(() => {
                    button.innerHTML = originalIcon;
                }, 2000);
            });
        }
    </script>
</body>
</html>